import cairoplot
import glob
import shutil
def get_hit_rate(f):
  f.readline() # Saco el header FIFO, MRU, ETc..
  return float(f.readline().split(':')[-1])

def process_one_pool_size(f):
  line = f.readline() # BufferPool size: 203
  #Spliteo, obtengo el ultimo, y lo convierto a numero
  ret = {}
  ret["buffer_size"] = int(line.split(':')[-1])

  ret["fifo"] = get_hit_rate(f)
  ret["lru"] = get_hit_rate(f)
  ret["mru"] = get_hit_rate(f)
  ret["touch"] = get_hit_rate(f)

  ret["data"] = [ret["fifo"], ret["lru"], ret["mru"], ret["touch"]]
  return ret


  
def get_data_and_sizes(f, different_pool_sizes):
  f.readline() # Grafico 1 - AGRUPADO POR BUFFERSIZE:
  
  total_data = []
  x_labels = []
  for i in range(different_pool_sizes):
    f.readline() # saco -------------------------------------------
    pool_size_data = process_one_pool_size(f)
    total_data.append(pool_size_data["data"])
    x_labels.append(pool_size_data["buffer_size"])

  sizes = map(lambda x: str(x), x_labels)
  total_data.append([0.0, 0.0, 0.0, 0.0,1.0])
  sizes.append("Reference")
  return total_data, sizes
 
def process_by_pool_size(f, different_pool_sizes):
  total_data, y_labels = get_data_and_sizes(f, different_pool_sizes)
  i = 0
  for some_data in total_data:
    da_data = map(lambda x: str(x), some_data)
    print " & ".join([y_labels[i]] + da_data) + " \\\\"
    i+=1
  x_labels = [ "0", "0.2", "0.4", "0.6", "0.8", "1.0" ]

  output_filename = f.name.split('.')[0]+'.png'
  colors = ["red", "yellow", "blue", "green", "white"]
  #cairoplot.horizontal_bar_plot ( output_filename, total_data, 400, 300, border = 20, y_labels = y_labels, display_values = False, grid = True, x_labels = x_labels, series_labels = ["fifo", "lru", "mru", "touch"], colors = colors )
  shutil.copyfile(output_filename, "../../informe/diagramas/"+output_filename)



def process_file(filename, different_pool_sizes = 5):
  print("Processing %s" % filename)
  f = open(filename)
  process_by_pool_size(f, different_pool_sizes)
  #process_by_strategy(f)
  f.close()

def main():
  process_file("grafico1.txt")
  process_file("grafico2.txt")
  process_file("grafico3.txt", 4)

if __name__ == "__main__": 
  main()